package vow.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JSplitPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JList;
import java.awt.Insets;
import javax.swing.border.TitledBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.border.BevelBorder;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.ListSelectionModel;

import ontology.models.negotiation.Part;

import dispute.NegotiationRoom;
import dispute.Round;

import agents.BlackBoard;

import vow.gui.models.GeneralInfoModel;
import vow.gui.models.PartieListModel;
import vow.gui.models.RoomListModel;
import vow.gui.models.RoundListModel;
import vow.gui.renderer.PartiesEntryRenderer;
import vow.gui.renderer.RoomEntryRenderer;
import vow.gui.renderer.RoundEntryRenderer;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTable;

public class BlackBoardGUI extends JFrame {

	private JPanel contentPane;
	private JList partiesList;
	private JList roomList;
	private JTextPane consoleOutput;
	private BlackBoard agent;
	private JTable generalInfoTable;
	private JTextPane roundInfo;
	private JList roundsList;

	/**
	 * Create the frame.
	 */
	public BlackBoardGUI(BlackBoard agent) {
		this.agent = agent;
		initComponents();
		
		
	}

	private void initComponents() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setContentPane(contentPane);
		contentPane.setLayout(new CardLayout(0, 0));
		
		JSplitPane splitPane = new JSplitPane();
		contentPane.add(splitPane, "name_1157831168358477");
		
		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{42, 0};
		gbl_panel.rowHeights = new int[]{14, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		JLabel lblConflitos = new JLabel("Conflitos");
		GridBagConstraints gbc_lblConflitos = new GridBagConstraints();
		gbc_lblConflitos.insets = new Insets(0, 0, 5, 0);
		gbc_lblConflitos.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblConflitos.gridx = 0;
		gbc_lblConflitos.gridy = 0;
		panel.add(lblConflitos, gbc_lblConflitos);
		
		roomList = new JList();
		roomList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2){
					NegotiationRoom roomSelected = (NegotiationRoom) roomList.getSelectedValue();
					showNegotiationRoom(roomSelected);
				}
			}
		});
		roomList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		roomList.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0)), new BevelBorder(BevelBorder.LOWERED, null, null, null, null)));
		refreshNegotiationRooms();
		GridBagConstraints gbc_roomList = new GridBagConstraints();
		gbc_roomList.gridheight = 3;
		gbc_roomList.fill = GridBagConstraints.BOTH;
		gbc_roomList.gridx = 0;
		gbc_roomList.gridy = 1;
		panel.add(roomList, gbc_roomList);
		
		JPanel panel_1 = new JPanel();
		splitPane.setRightComponent(panel_1);
		panel_1.setLayout(new CardLayout(0, 0));
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		panel_1.add(tabbedPane, "name_1158514825421051");
		
		JPanel generalInfoPanel = new JPanel();
		tabbedPane.addTab("Informa\u00E7\u00E3o Geral", null, generalInfoPanel, null);
		generalInfoPanel.setLayout(new CardLayout(0, 0));
		
		generalInfoTable = new JTable();
		generalInfoPanel.add(generalInfoTable, "name_201692945460189");
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("Partes Envolvidas", null, panel_2, null);
		panel_2.setLayout(new CardLayout(0, 0));
		
		JSplitPane splitPane_2 = new JSplitPane();
		panel_2.add(splitPane_2, "name_1158800589811306");
		
		JPanel panel_4 = new JPanel();
		splitPane_2.setRightComponent(panel_4);
		
		JLabel lblInfo = new JLabel("Info");
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4.setHorizontalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblInfo)
					.addContainerGap(300, Short.MAX_VALUE))
		);
		gl_panel_4.setVerticalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addComponent(lblInfo)
					.addContainerGap(203, Short.MAX_VALUE))
		);
		panel_4.setLayout(gl_panel_4);
		
		JPanel panel_5 = new JPanel();
		splitPane_2.setLeftComponent(panel_5);
		GridBagLayout gbl_panel_5 = new GridBagLayout();
		gbl_panel_5.columnWidths = new int[]{31, 0};
		gbl_panel_5.rowHeights = new int[]{14, 0, 0, 0, 0};
		gbl_panel_5.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_5.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		panel_5.setLayout(gbl_panel_5);
		
		JLabel lblPartes = new JLabel("Partes");
		GridBagConstraints gbc_lblPartes = new GridBagConstraints();
		gbc_lblPartes.insets = new Insets(0, 0, 5, 0);
		gbc_lblPartes.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblPartes.gridx = 0;
		gbc_lblPartes.gridy = 0;
		panel_5.add(lblPartes, gbc_lblPartes);
		
		partiesList = new JList();
		GridBagConstraints gbc_partiesList = new GridBagConstraints();
		gbc_partiesList.gridheight = 3;
		gbc_partiesList.fill = GridBagConstraints.BOTH;
		gbc_partiesList.gridx = 0;
		gbc_partiesList.gridy = 1;
		panel_5.add(partiesList, gbc_partiesList);
		
		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("Informa\u00E7\u00E3o da Ronda", null, panel_3, null);
		//panel_3.setLayout(new CardLayout(0, 0));
		
		JSplitPane splitPane_1 = new JSplitPane();
		panel_3.add(splitPane_1);
		
		roundsList = new JList();
		roundsList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2){
					Round round = (Round) roundsList.getSelectedValue();
					showRoundInfo(round);
				}
			}
		});
		splitPane_1.setLeftComponent(roundsList);
		
		roundInfo = new JTextPane();
		splitPane_1.setRightComponent(roundInfo);
		
		JPanel panel_6 = new JPanel();
		tabbedPane.addTab("Consola", null, panel_6, null);
		panel_6.setLayout(new CardLayout(0, 0));
		
		consoleOutput = new JTextPane();
		consoleOutput.setFont(new Font("Tahoma", Font.PLAIN, 11));
		consoleOutput.setForeground(Color.BLACK);
		consoleOutput.setEditable(false);
		panel_6.add(consoleOutput, "name_1220799042013692");
	}

	public void refreshNegotiationRooms() {
		Collection<NegotiationRoom> rooms = agent.getNegotiationRooms();
		if(rooms.isEmpty()){
			System.out.println("No negotiation rooms to refresh!!!");
		}
		else{
			for(NegotiationRoom n : rooms)
				System.out.println("room: "+n.toShortString());
			roomList.setModel(new RoomListModel(rooms));
			roomList.setCellRenderer(new RoomEntryRenderer());
		}		
	}
	
	private void showRoundInfo(Round round){
		roundInfo.setText(round.toShortString());
	}
	
	public void showNegotiationRoom(NegotiationRoom room){
		updateGeneralInfo(room);
		updatePartsInfo(room);
		updateRoundInfo(room);
	}

	private void updateRoundInfo(NegotiationRoom room) {
		roundsList.setModel(new RoundListModel(room.getRounds()));
		roundsList.setCellRenderer(new RoundEntryRenderer());
	}

	private void updatePartsInfo(NegotiationRoom room) {
		partiesList.setModel(new PartieListModel(new ArrayList<Part>(room.getNegotiators().getPanel().values())));
		partiesList.setCellRenderer(new PartiesEntryRenderer());
	}

	private void updateGeneralInfo(NegotiationRoom room) {
		generalInfoTable.setModel(new GeneralInfoModel(room.getProposals()));		
	}

	public void println(final String string) {
		EventQueue.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				String t = consoleOutput.getText().concat(string+"\n\n");
				consoleOutput.setText(t);				
			}
		});
	}
}
